package net.flyinggroup.gateway.controller;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

/**
 * 使用Mono的just，感觉这个意义不大，而且如果方法可以直接反馈信息，是否需要使用Mono或者Flux？
 * 针对存在延时的情况，均可以考虑使用Mono/Flux。比如有网络调用等等。
 */

@Controller
public class MainController {

    @GetMapping("/")
    public String getIndex() {
        return "index";
    }

    @GetMapping("/home")
    public String getHome() {
        return "home";
    }

    @GetMapping("/whoami")
    @ResponseBody
    public Mono<Map<String, Object>> index(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient client, @AuthenticationPrincipal OAuth2User user) {
        Map<String, Object> model = new HashMap<>();
        model.put("clientName", client.getClientRegistration().getClientName());
        model.put("userName", user.getName());
        model.put("userAttributes", user.getAttributes());
        return Mono.just(model);
    }
}